//
//  CAU256.m
//  Test
//
//  Created by xgc on 1/5/18.
//  Copyright © 2018 xgc. All rights reserved.
//

#import "CAU256.h"
#include "../utils/bcmath_stl.h"

@implementation CAU256

+(id)initWithString: (NSString*)data
{
    BCMath::bcscale(18);
    CAU256* au256 = [[CAU256 alloc] init];
    au256.value = data;
    
    return au256;
}

-(void)addWith:(NSString*) rhs
{
    
    BCMath lhsValue([self.value UTF8String]);
    BCMath rhsValue([rhs UTF8String]);
    
    BCMath resValue = lhsValue + rhsValue;
    
    self.value = [NSString stringWithCString:resValue.toString().c_str() encoding:[NSString defaultCStringEncoding]];
    
}

-(void)subWith:(NSString*) rhs
{
    BCMath lhsValue([self.value UTF8String]);
    BCMath rhsValue([rhs UTF8String]);
    
    BCMath resValue = lhsValue - rhsValue;
    
    self.value = [NSString stringWithCString:resValue.toString().c_str() encoding:[NSString defaultCStringEncoding]];
}

-(void)mulWith:(NSString*) rhs
{
    BCMath lhsValue([self.value UTF8String]);
    BCMath rhsValue([rhs UTF8String]);
    
    BCMath resValue = lhsValue * rhsValue;
    
    self.value = [NSString stringWithCString:resValue.toString().c_str() encoding:[NSString defaultCStringEncoding]];
}

-(void)divWith:(NSString*) rhs
{
    BCMath lhsValue([self.value UTF8String]);
    BCMath rhsValue([rhs UTF8String]);
    
    BCMath resValue = lhsValue / rhsValue;
    
    self.value = [NSString stringWithCString:resValue.toString().c_str() encoding:[NSString defaultCStringEncoding]];
}

-(void)powWith:(NSString*) rhs
{
    BCMath lhsValue([self.value UTF8String]);
    BCMath rhsValue([rhs UTF8String]);
    
    BCMath resValue = lhsValue ^ rhsValue;
    
    self.value = [NSString stringWithCString:resValue.toString().c_str() encoding:[NSString defaultCStringEncoding]];
}

-(NSString*)toString
{
    return self.value;
}

-(NSString*)getIntPart
{
    BCMath lhsValue([self.value UTF8String]);
    std::string strValue = lhsValue.getIntPart();
    
    return [NSString stringWithCString:strValue.c_str() encoding:[NSString defaultCStringEncoding]];
}

-(NSString*)getDecPart
{
    BCMath lhsValue([self.value UTF8String]);
    std::string strValue = lhsValue.getDecPart();
    
    return [NSString stringWithCString:strValue.c_str() encoding:[NSString defaultCStringEncoding]];
}

-(id)init {
    if (self = [super init]) {
        self.value = @"0.0";
    }
    return self;
}

@end
